package com.tj.jinzao.widgets;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;


/**
 * 通知dialog
 * Created by wjy on 2018/4/25.
 */
public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder implements View.OnClickListener {
        private Context mContext;
        private String mTitle;
        private int mTitleColor = -1;
        private int mTitleBackGround = -1;
        private CharSequence mMessage;
        private String mPositiveText;
        private String mNegativeText;
        private boolean mNeedCenter;
        private View mContentView;
        private OnClickListener mOnClickListener;

        private CustomDialog mDialog;

        private View mLayout;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder setMessage(CharSequence message) {
            this.mMessage = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param messageId
         * @return
         */
        public Builder setMessage(int messageId) {
            this.mMessage = mContext.getText(messageId);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param titleId
         * @return
         */
        public Builder setTitle(int titleId) {
            this.mTitle = (String) mContext.getText(titleId);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.mTitle = title;
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @return
         */
        public Builder setTitleColor(int Color) {
            this.mTitleColor = Color;
            return this;
        }

        public Builder setTitleBackGround(int backGround) {
            this.mTitleBackGround = backGround;
            return this;
        }

        private LayoutParams mContentLayoutParams;

        public Builder setContentViewWithLayoutParams(View v, LayoutParams layoutParams) {
            this.mContentView = v;
            mContentLayoutParams = layoutParams;
            return this;
        }

        public Builder setContentView(View v) {
            this.mContentView = v;
            return this;
        }

        public Builder setOnClickListener(OnClickListener listener) {
            this.mOnClickListener = listener;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param textId
         * @return
         */
        public Builder setPositiveButton(int textId) {
            this.mPositiveText = (String) mContext
                    .getText(textId);
            return this;
        }

        public Builder setPositiveButton(String text) {
            this.mPositiveText = text;
            return this;
        }

        public Builder setNegativeButton(int textId) {
            this.mNegativeText = (String) mContext
                    .getText(textId);
            return this;
        }

        public Builder setNegativeButton(String text) {
            this.mNegativeText = text;
            return this;
        }

        public Builder setCenter(boolean needCenter) {
            this.mNeedCenter = needCenter;
            return this;
        }

        public CustomDialog create() {
            return this.create(R.layout.dialog_custom);
        }

        public CustomDialog create(int layoutRes) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            mDialog = new CustomDialog(mContext, R.style.Dialog);
            mLayout = inflater.inflate(layoutRes, null);
            mDialog.addContentView(mLayout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title

//            try {
//                TextView titleView = mLayout.findViewById(R.id.dialog_title);
//                if (mTitleColor != -1) {
//                    titleView.setBackgroundColor(mTitleColor);
//                    titleView.setTextColor(mContext.getResources().getColor(R.color.custom_button_text_color));
//                    titleView.setGravity(Gravity.CENTER);
//                }
//                if (mTitleBackGround != -1) {
//                    titleView.setBackgroundResource(mTitleBackGround);
//                    titleView.setTextColor(mContext.getResources().getColor(R.color.custom_button_text_color));
//                    titleView.setGravity(Gravity.CENTER);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            // set the button
            if (mNegativeText != null) {
                Button negativeButton = (Button) mLayout.findViewById(R.id.dialog_negative_button);
                negativeButton.setText(mNegativeText);
                negativeButton.setOnClickListener(this);
                Button positiveButton = (Button) mLayout.findViewById(R.id.dialog_positive_button);
                positiveButton.setText(mPositiveText);
                positiveButton.setOnClickListener(this);
            } else {
                mLayout.findViewById(R.id.double_button_layout).setVisibility(View.GONE);
                Button positiveButton = (Button) mLayout.findViewById(R.id.dialog_solo_button);
                positiveButton.setText(mPositiveText);
                positiveButton.setOnClickListener(this);
                positiveButton.setVisibility(View.VISIBLE);
            }
            // set the content message
            if (mMessage != null) {
                TextView content = (TextView) mLayout.findViewById(R.id.dialog_message);
                content.setText(mMessage);
//                if (mNeedCenter) {
//                    content.setGravity(Gravity.CENTER);
//                } else {
//                    content.setGravity(Gravity.LEFT);
//                }
                content.setMovementMethod(new ScrollingMovementMethod());
            } else if (mContentView != null) {
                // if no message set
                // chat_input_add the contentView to the dialog body
                ((LinearLayout) mLayout.findViewById(R.id.dialog_content))
                        .removeAllViews();
                ((LinearLayout) mLayout.findViewById(R.id.dialog_content))
                        .addView(mContentView, mContentLayoutParams == null ?
                                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) : mContentLayoutParams);
            }
//            if (TextUtils.isEmpty(mTitle)) {
//                View v = mLayout.findViewById(R.id.dialog_title);
//                if (v != null) v.setVisibility(View.GONE);
//                //解决设置TextView行距导致的文字不居中
//                TextView content = (TextView) mLayout.findViewById(R.id.dialog_message);
//                if (content != null) {
//                    content.setPadding(0, mContext.getResources().
//                            getDimensionPixelOffset(R.dimen.cardview_default_radius), 0, 0);
//                }
//            } else {
//                ((TextView) mLayout.findViewById(R.id.dialog_title)).setText(mTitle);
//            }
            mDialog.setContentView(mLayout);
            return mDialog;
        }

        public View getLayout() {
            return mLayout;
        }

        @Override
        public void onClick(View v) {
            if (mOnClickListener != null) {
                int id = v.getId();
                if (R.id.dialog_negative_button == id) {
                    mOnClickListener.onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
                } else if (R.id.dialog_positive_button == id || R.id.dialog_solo_button == id) {
                    mOnClickListener.onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
                }
            }
        }
    }
}
